package org.example.service.impl;

import lombok.RequiredArgsConstructor;
import org.example.common.config.DefaultBookFactory;
import org.example.common.model.Book;
import org.example.request.BookRequest;
import org.example.service.BookService;
import org.example.service.ValidationStrategy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.bind.ValidationException;
import java.util.List;

@Service
@RequiredArgsConstructor
public class BookServiceImpl implements BookService {
    @Resource
    private List<ValidationStrategy> strategies;
    @Override
    public Book getBookById(Long id) {
        return DefaultBookFactory.getInstance().findById(id);
    }

    @Override
    public Long createBook(BookRequest book) throws ValidationException {
        for (ValidationStrategy strategy : strategies) {
            strategy.validate(book);
        }
        return DefaultBookFactory.getInstance().createBook(book);
    }

    @Override
    public void updateBook(Long id, BookRequest book) throws ValidationException {
        for (ValidationStrategy strategy : strategies) {
            strategy.validate(book);
        }
        DefaultBookFactory.getInstance().updateById(id, book);
    }

    @Override
    public void deleteBook(Long id) {
        DefaultBookFactory.getInstance().deleteById(id);
    }
}
